<?php

class Rs extends Controller {
	const INVALID_DATA = "invalid data";
	const FAILURE = "failure";
        const NO_DATA = "no data returned";
        const SUCCESS = "success";

        const ERROR = 1;
        const WARNING = 2;
        const NOTICE = 3;
        const UNKNOWN = 4;
        const STRICT = 5;
        
        function Rs(){
		parent::Controller();

		$this->load->model('Model_controlpanel');
	}
	
	function addDomain(){
		if($domain = $this->validate($_POST['domain'], FILTER_VALIDATE_URL)){
			if($this->Model_controlpanel->addDomain($domain)){
				$domains = $this->Model_controlpanel->getDomains();
				$result['feedback'] = $domains;	
			}else{
				$result['feedback'] = self::FAILURE;
			}
			
		}else{
			// does not validate
			$result['feedback'] = self::INVALID_DATA;
		}
		
		print json_encode($result);
	}
	
	function generateReport($messages){
            $report="";
            $x=1;
            foreach($messages as $message){
                switch($message['message_type']){
                    case 1:
                        $class = "php_error";
                        break;

                    case 2:
                        $class = "php_warning";
                        break;

                    case 3:
                        $class = "php_notice";
                        break;

                    case 4:
                        $class = "php_unknown";
                        break;

                    case 5:
                        $class = "php_strict";
                        break;
                }
                $report .= "<div id='".$message['message_id']."' class='" . $class . " sm_message'><span class='expand'>[+]</span>";
                $report .= sprintf("%s:  %s in %s on line %d", $message['type'], stripslashes($message['message']), $message['message_file'], $message['message_line']);
                $report .= "<div class='dateTime'>Date: ". date('m-d-y H:i:s', strtotime($message['date_created'])) . "</div>";
                $report .= "<div class=''>URL: " . $message['php_self'] . "</div>";
                $report .= "<div class='messageOptions'><span class='messageDelete'>delete</span></div>";
                $report .= "</div>";
            }
            return $report;
	}
	
	function viewReport(){
            $error_type_array = null;
            $page = $_POST['page'];
            $records = $_POST['rowsPerPage'];
            $timestamp = date("Y-m-d H:i:s", $_POST['timestamp']);

            if(isset($_POST['id'])){
                $id = $_POST['id'];
                $error_type_array = explode(",", $id);
            }

            if($domain_name = $this->validate($_POST['domain_name'], FILTER_SANITIZE_STRING)){
                $messages = $this->Model_controlpanel->getMessages($domain_name, $page, $records, $error_type_array, $timestamp);
                if(!empty($messages) && $messages){
                    $result['report'] = $this->generateReport($messages); // this function should be a self contained library
                    $total = $this->Model_controlpanel->getMessageTotals($domain_name, $error_type_array, $timestamp);
                    $result['total'] = $total['total'];
                    $result['total_error'] = $this->Model_controlpanel->getTotalByType($domain_name, self::ERROR, null, null, $timestamp);
                    $result['total_warning'] = $this->Model_controlpanel->getTotalByType($domain_name, self::WARNING, null, null, $timestamp);
                    $result['total_strict'] = $this->Model_controlpanel->getTotalByType($domain_name, self::STRICT, null, null, $timestamp);
                    $result['total_notice'] = $this->Model_controlpanel->getTotalByType($domain_name, self::NOTICE, null, null, $timestamp);
                }else{
                    $result['report'] = self::NO_DATA;
                    $result['total'] = 0;
                }
            }else{
                $result['report'] = self::INVALID_DATA;
            }

            print json_encode($result);
	}

        function editMessage(){
            $action = $_POST['action'];
            $timestamp = date("Y-m-d H:i:s", $_POST['timestamp']);

            if($action == "delete"){
                $message_id = $_POST['message_id'];
                $response = $this->Model_controlpanel->deleteMessage($message_id);

                if($response){
                    $error_type_array = null;
                    $page = $_POST['page'];
                    $records = $_POST['rowsPerPage'];

                    if(isset($_POST['checkbox_id'])){
                        $id = $_POST['checkbox_id'];
                        $error_type_array = explode(",", $id);
                    }

                    if($domain_name = $this->validate($_POST['domain_name'], FILTER_SANITIZE_STRING)){
                        do{
                            $messages = $this->Model_controlpanel->getMessages($domain_name, $page, $records, $error_type_array, $timestamp);
                            if(!empty($messages) && $messages){
                                $result['report'] = $this->generateReport($messages); // this function should be a self contained library
                                $total = $this->Model_controlpanel->getMessageTotals($domain_name, $error_type_array, $timestamp);
                                $result['total'] = $total['total'];
                                $result['total_error'] = $this->Model_controlpanel->getTotalByType($domain_name, self::ERROR, null, null, $timestamp);
                                $result['total_warning'] = $this->Model_controlpanel->getTotalByType($domain_name, self::WARNING, null, null, $timestamp);
                                $result['total_strict'] = $this->Model_controlpanel->getTotalByType($domain_name, self::STRICT, null, null, $timestamp);
                                $result['total_notice'] = $this->Model_controlpanel->getTotalByType($domain_name, self::NOTICE, null, null, $timestamp);
                                $result['page'] = $page;
                            }else{
                                $result['report'] = self::NO_DATA;
                                $result['total'] = 0;
                            }
                            $page--;
                        }while($page > 0 && (empty($messages) || !$messages));
                    }else{
                        $result['report'] = self::INVALID_DATA;
                    }

                    print json_encode($result);
                }else{
                    print json_encode(array("feedback" => self::FAILURE, "id" => $message_id));
                }
            }else if($action == "clear"){
                if($domain_name = $this->validate($_POST['domain_name'], FILTER_SANITIZE_STRING)){
                    if($this->Model_controlpanel->clearMessages($domain_name)){
                        $result['report'] = self::NO_DATA;
                        $result['total'] = 0;
                    }else{
                        $result['report'] = self::FAILURE;
                    }
                }else{
                    $result['report'] = self::INVALID_DATA;
                }

                print json_encode($result);
            }
        }

	function validate($data, $filter_constant){
            return filter_var($data, $filter_constant);
	}

        function viewStats(){
            $range = $_POST['range'];
            $domain_name = $_POST['domain_name'];

            // set parameters for date range stats
            if($range == "all"){
                $startDate = null;
                $endDate = null;
            }else if($range == "today"){
                $startDate = date("Y-m-d 00:00");
                $endDate = date("Y-m-d 59:59");
            }else if($range == "week"){
                $startDate = date("Y-m-d 00:00", time() - 60*60*24*6);
                $endDate = date("Y-m-d 59:59");
            }else if($range == "month"){
                $startDate = date("Y-m-d 00:00", mktime(0, 0, 0, date('m'), 1));
                $endDate = date("Y-m-d 59:59", mktime(0, 0, 0, date('m'), 31));
            }

            // set parameters for url stats
            // get records of most occurring urls
            $urls = $this->Model_controlpanel->getMostOccurringUrls($domain_name, $startDate, $endDate);

            $result['urls'] = $this->generateUrlStats($urls);
            $result['total_error'] = $this->Model_controlpanel->getTotalByType($domain_name, self::ERROR, $startDate, $endDate);
            $result['total_warning'] = $this->Model_controlpanel->getTotalByType($domain_name, self::WARNING, $startDate, $endDate);
            $result['total_strict'] = $this->Model_controlpanel->getTotalByType($domain_name, self::STRICT, $startDate, $endDate);
            $result['total_notice'] = $this->Model_controlpanel->getTotalByType($domain_name, self::NOTICE, $startDate, $endDate);

            print json_encode($result);
        }

        function generateUrlStats($urls){
            $stats="<table border='1'>";
            foreach($urls as $url){
                $stats .= "<tr><td>" . $url['total'] . "</td><td>" . $url['php_self'] . "</td></tr>";
            }
            $stats .= "</table>";
            return $stats;
        }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */